<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="商品名称">
                <a-input allow-clear placeholder="请输入商品名称" v-model="queryParam.goods_name" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="代理名称">
                <a-input allow-clear placeholder="请输入代理名称" v-model="queryParam.admin_name" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="供货商">
                <div class="flex">
                  <a-input allow-clear placeholder="请输入供货商" v-model="queryParam.supplier_name"></a-input>
                  <a-button type="primary" @click="tablePageChange(1)">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :bordered="false">
      <!-- 代理所有 -->
      <!-- 供货商添加查看 不可删除、修改 -->
      <!-- 平台查看 -->
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="0"> 未审核 </a-radio-button>
          <a-radio-button value="1"> 已通过 </a-radio-button>
          <a-radio-button value="2"> 已拒绝 </a-radio-button>
        </a-radio-group>

        <div>
          <a-button type="primary" @click="visible = true">+ 新增</a-button>
        </div>
      </div>

      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <div slot="product" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.pic_url" alt="" />
            </div>
            <div class="info">
              <a-tooltip>
                <template slot="title"> {{ record.name }} </template>
                <span>{{ record.name }}</span>
              </a-tooltip>
            </div>
          </div>
        </div>

        <div slot="supplier" slot-scope="text, record">
          <div class="name" v-if="record.supplier">
            <div class="pic">
              <img :src="record.supplier.logo" alt="" />
            </div>
            <div class="info">
              <a-tooltip>
                <template slot="title"> {{ record.supplier.name }} </template>
                <span>{{ record.supplier.name }}</span>
              </a-tooltip>
            </div>
          </div>
        </div>

        <span slot="status" slot-scope="text">
          <div class="tagBox">
            <span class="tag" :style="text | orderStatus"></span>
            <span>{{ getStatus(text) }} </span>
          </div>
        </span>

        <div slot="action" slot-scope="scope">
          <a-button type="link" size="small" @click="handle('remove', scope)">删除</a-button>
          <a-divider v-if="parseInt(scope.bind_status) === 0" type="vertical" style="margin: 0 1px" />

          <a-dropdown v-if="parseInt(scope.bind_status) === 0">
            <a-button type="link" size="small">更多<a-icon type="down" /></a-button>
            <a-menu slot="overlay">
              <template>
                <a-menu-item>
                  <a-button type="link" size="small" @click="handle('change', scope, 1)">同意</a-button>
                </a-menu-item>
                <a-menu-item>
                  <a-button type="link" size="small" @click="handle('change', scope, 2)">拒绝</a-button>
                </a-menu-item>
              </template>
            </a-menu>
          </a-dropdown>
        </div>
      </a-table>
    </a-card>

    <a-modal
      :title="form.id ? '修改' : '新增'"
      width="800px"
      v-model="visible"
      @ok="submit"
      :afterClose="closeModal"
      :destroyOnClose="true"
      :confirmLoading="btnLoading"
    >
      <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }">
        <a-form-model-item label="选择商品" prop="cloud_goods_id">
          <div class="flex">
            <a-input class="inp" disabled v-model="cloud_goods_name" placeholder="请选择商品" />
            <a-button type="primary" @click="handle('open', 'product')">选择</a-button>
          </div>
        </a-form-model-item>
        <a-form-model-item label="选择代理" prop="admin_id">
          <div class="flex">
            <a-input class="inp" disabled v-model="admin_name" placeholder="请选择代理" />
            <a-button type="primary" @click="handle('open', 'admin')">选择</a-button>
          </div>
        </a-form-model-item>
      </a-form-model>

      <productListPop title="选择商品" ref="product" @select="(e) => select('cloud_goods_id', e)"></productListPop>
      <dailiListPop ref="admin" @select="(e) => select('admin_id', e)"></dailiListPop>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { getBindGoodsList, applyBindFrontAgentGoods, bindFrontAgentGoods, delBindFrontAgentGoods } from '@/api/daili.js'
import productListPop from './components/productListPop.vue'
import dailiListPop from './components/dailiListPop.vue'
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '商品信息',
    scopedSlots: {
      customRender: 'product',
    },
    width: 280,
  },
  {
    title: '供货商信息',
    scopedSlots: {
      customRender: 'supplier',
    },
    width: 200,
  },
  {
    title: '售价',
    dataIndex: 'price',
    sorter: (a, b) => parseFloat(a.price) - parseFloat(b.price),
  },
  {
    title: '库存',
    dataIndex: 'num',
    sorter: (a, b) => parseFloat(a.num) - parseFloat(b.num),
  },
  {
    title: '推广代理',
    dataIndex: 'user_name',
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    width: 180,
    scopedSlots: {
      customRender: 'action',
    },
  },
]

export default {
  components: {
    productListPop,
    dailiListPop,
  },

  data() {
    return {
      queryParam: {
        status: '-1',
        goods_name: '',
        admin_name: '',
        supplier_name: '',
      },

      list: [],
      columns,

      visible: false,
      btnLoading: false,
      form: {
        cloud_goods_id: '',
        admin_id: '',
      },

      cloud_goods_name: '',
      admin_name: '',
      rules: {
        cloud_goods_id: [{ required: true, message: '请选择商品', trigger: 'change' }],
        admin_id: [{ required: true, message: '请选择代理', trigger: 'change' }],
      },
    }
  },

  created() {
    this.tablePageChange(1)
  },

  computed: {
    getStatus() {
      return (status) => {
        let str

        switch (parseInt(status)) {
          case 0:
            str = '未审核'
            break

          case 1:
            str = '已通过'
            break

          case 2:
            str = '已拒绝'
            break
        }
        return str
      }
    },
  },

  filters: {
    orderStatus(val) {
      if (val == 0) {
        return 'background-color:#faad14;'
      }

      if (val == 1) {
        return 'background-color:#65c817;'
      }

      if (val == 2) {
        return 'background-color:#ff0000;'
      }
    },
  },

  methods: {
    // 分页请求
    tablePageChange(e) {
      this.queryParam.page = e
      this.getList()
    },

    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      let { queryParam } = this
      const res = await getBindGoodsList(queryParam)
      if (res.code == 0) {
        this.list = res.data.list
        console.log('list,', this.list)
        this.totalCount = res.data.count
      }
      hide()
    },

    handle(type, item, status) {
      switch (type) {
        case 'remove':
          this.bindDel(item.id)
          break

        case 'change':
          this.bindStatus(item.id, status)
          break

        case 'open':
          this.$refs[item].visible = true
          break
      }
    },

    callback(res) {
      if (!res.code) {
        this.getList()
        this.$message.success(res.msg)
        return
      }

      this.$message.error(res.msg)
    },

    bindDel(ids) {
      this.$confirm({
        title: '是否删除供货商？',
        onOk: async () => {
          const res = await delBindFrontAgentGoods({ id: ids })
          this.callback(res)
        },
        onCancel() {},
      })
    },

    bindStatus(ids, status) {
      this.$confirm({
        title: '是否确认当前操作？',
        onOk: async () => {
          const res = await applyBindFrontAgentGoods({ id: ids, status })
          this.callback(res)
        },
        onCancel() {},
      })
    },

    //
    submit() {
      this.$refs['ruleForm'].validate(async (valid) => {
        if (valid) {
          let form = JSON.parse(JSON.stringify(this.form))

          this.btnLoading = true
          let res = await bindFrontAgentGoods(form)
          if (res.code == 0) {
            this.$message.success('操作成功')
            this.visible = false
            this.tablePageChange(1)
            this.cloud_goods_name = ''
            this.admin_name = ''
          } else {
            this.$message.error(res.msg)
          }
          this.btnLoading = false
        } else {
          return false
        }
      })
    },

    closeModal() {
      this.$refs['ruleForm'].resetFields()
      this.cloud_goods_name = ''
      this.supplier_name = ''
    },

    select(el, e) {
      switch (el) {
        case 'cloud_goods_id':
          this.form.cloud_goods_id = e.id
          this.cloud_goods_name = e.name
          break

        case 'admin_id':
          this.form.admin_id = e.id
          this.admin_name = e.name
          break
      }
    },
  },
}
</script>
<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    margin-top: 11px;
    display: flex;
    align-items: center;
    margin-right: 15px;
  }
  .ant-btn {
    margin-top: 11px;
  }
}

.flex {
  .inp {
    margin-right: 16px;
  }
}

.tagBox {
  display: flex;
  align-items: center;
}

.tag {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-right: 4px;
}

.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
</style>
